package com.application.controller;


import com.application.domain.User;
import com.application.domain.request;
import com.application.service.ActivityService;
import com.application.service.CommunityService;
import com.application.service.RequestService;
import com.application.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/request")
@Api(tags = "用户申请参加活动")
public class RequestController {

    @Resource
    RequestService requestService;
    @Resource
    UserService userService;

    @Resource
    CommunityService communityService;

    @Resource
    ActivityService activityService;

    @ApiOperation("用户添加申请表到数据库中")
    @PutMapping
    public Result Insert(request Request){
        int save = requestService.save(Request);
        String mes = save !=0 ?"申请成功":"申请失败，请重新尝试";
        int code = save !=0 ? Code.SAVE_OK:Code.SAVE_ERR;
        return new Result(code,save,mes);
    }


    @ApiOperation("删除申请列表")
    @DeleteMapping
    public Result deleteById(@RequestParam String openid){
        int deleteById = requestService.deleteById(openid);
        String mes = deleteById !=0 ?"删除成功":"删除失败，请重新尝试";
        int code = deleteById !=0 ? Code.DELETE_OK:Code.DELETE_ERR;
        return new Result(code,deleteById,mes);
    }


    @ApiOperation("查询申请活动的用户")
    @GetMapping
    //  name  代表  活动的名字
    public Result SelectByName(@RequestParam String name,@RequestParam String openid){
        User user = userService.selectById(openid);
        //  不仅仅需要是管理员   而且还是需要 当前活动对应的社团
        if(user.getLevel()>=2
                &&
                activityService.getCommunityByName(name).equals(user.getCommunity()))
        {
            List<request> requests = requestService.selectByName(name);
            String mes = requests!=null?"查询成功":"当前没有人选择该活动";
            return new Result(Code.SELECT_OK,requests,mes);
        }else {
            return new Result(Code.SELECT_ERR,null,"非工作人员无法查看");
        }
    }


    @PutMapping("/ActivityCheck")
    @ApiOperation("通过审核申请活动的人员")
    public Result ActivityCheck(@RequestParam String openid){
        int updateByOpenid = requestService.ACT_CheckByOpenid(openid);
        String mes = updateByOpenid !=0 ? "审核通过":"审核失败，请重新尝试";
        int code = updateByOpenid !=0 ? Code.UPDATE_OK:Code.UPDATE_ERR;

        return new Result(code,updateByOpenid,mes);
    }

    @PutMapping("/CommmunityCheck")
    @ApiOperation(value = "通过申请社团的请求")
    //  需要  通过的用户的  openid
    public Result CommunityCheck(@RequestParam String openid){
        int updateByOpenid = requestService.COM_CheckByOpenid(openid);
        //  首先对  request表中修改成功
        if(updateByOpenid != 0){
        //审核通过之后  就需要   将  user中的表格的  position和 flag 直接写为2设置 还有community
            String comName = requestService.selectComName(openid);
            int updateByLeader = userService.updateByLeader(comName, "工作人员", openid);
            String mes = updateByLeader != 0 ? "审核通过":"审核失败，请重新尝试";
            int code = updateByLeader != 0 ? Code.UPDATE_OK:Code.UPDATE_ERR;
            return new Result(code,updateByOpenid,mes);
        }

        return new Result(Code.UPDATE_ERR,null,"通过失败，请联系我们");
    }


    @PutMapping("/RefuseCommunityApply")
    @ApiOperation(value = "拒绝加入社团请求")
    public Result RefuseCommunityApply(@RequestParam String openid){
        // 需要删除request中的数据    而且修改  user表中的flag
        int updateFlagByOpenid = userService.updateFlagByOpenid(openid);
        int deleteById = requestService.deleteById(openid);
        String mes ;
        int code =0;
        if(updateFlagByOpenid !=0 && deleteById !=0){
            mes = "拒绝成功";
            code = Code.DELETE_OK;
        }else {
            mes = "拒绝失败，请重新尝试";
            code = Code.DELETE_ERR;
        }

        return  new Result(code,updateFlagByOpenid,mes);
    }


    @ApiOperation(value = "拒绝活动的申请")
    @PutMapping("/RefuseActivityApply")
    public Result RefuseActivityApply(@RequestParam String openid){
        int deleteById = requestService.deleteById(openid);

        String mes = deleteById !=0 ? "拒绝成功":"拒绝失败";
        int code = deleteById !=0 ? Code.DELETE_OK:Code.DELETE_ERR;
        return  new Result(code,deleteById,mes);
    }

}
